package com.xoa.service.equipment.impl;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.equipment.EquipmentMapper;
import com.xoa.dao.equipment.LimsEquipScraplistMapper;
import com.xoa.dao.equipment.LimsEquipScraplogMapper;
import com.xoa.model.department.Department;
import com.xoa.model.equipment.Equipment;
import com.xoa.model.equipment.LimsEquipScraplist;
import com.xoa.model.equipment.LimsEquipScraplog;
import com.xoa.model.users.Users;
import com.xoa.service.equipment.EquipmentScrappingManagementService;
import com.xoa.util.LimsJson;
import com.xoa.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @作者: 祝依泽
 * @创建日期： 2019/10/14
 * @类介绍: 设备报废管理
 */
@Service
public class EquipmentScrappingManagementServiceImpl implements EquipmentScrappingManagementService {

    @Autowired
    private LimsEquipScraplogMapper limsEquipScraplogMapper;

    @Autowired
    private LimsEquipScraplistMapper limsEquipScraplistMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * @作者: 祝依泽
     * @创建日期： 2019/10/14
     * @参数说明： String ids:报废申请id串
     * @方法介绍: 批量批准和单个批准共用方法
     */
    @Override
    public LimsJson ratify(HttpServletRequest request, String ids) {
        LimsJson limsJson = new LimsJson(1,"err");                                      //封装返回数据工具
        if (ids != null && !(("").equals(ids))) {
            String[] idArray = ids.split(",");                                                //将ids用逗号分隔为id并存入数组
            LimsEquipScraplog limsEquipScraplog = new LimsEquipScraplog();
            LimsEquipScraplist limsEquipScraplist = new LimsEquipScraplist();
            Equipment equipment = new Equipment();
            Users user = UserUtil.getUser(request);                                                  //获取当前用户
            Date date = new Date();                                                                  //获取当前时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");       //转换时间格式方法
            int count = 0;                                                                           //用来记录删除数据的条数
            for (String idarr : idArray) {
                int scraplogId = Integer.valueOf(idarr);                                             //将String类型id转化为Integer类型id
                limsEquipScraplog.setScraplogId(scraplogId);                                         //主键id
                limsEquipScraplog.setApprover(user.getUserName());
                limsEquipScraplog.setApproverTime(format.format(date));
                limsEquipScraplog.setApprovalStatus("2");
                limsEquipScraplogMapper.updateByPrimaryKeySelective(limsEquipScraplog);
                limsEquipScraplist.setScraplogId(scraplogId);                                         //存入报废申请id，根据报废申请id修改数据
                limsEquipScraplist.setEquipStatuasCode("EQUIP_SCRAPPED");                           //设为报废状态
                limsEquipScraplist.setOpera(user.getUserName());                                      //存入审批人
                limsEquipScraplist.setOperaTime(new Date());                                          //存入审批时间
                List<LimsEquipScraplist> limsEquipScraplistList = limsEquipScraplistMapper.selectScraplistByScraplogId(scraplogId); //查询报废申请下的所有报废设备
                equipment.setEquipStatuasId("EQUIP_SCRAPPED");                                      //存入报废的commonCode编码
                for (LimsEquipScraplist equipScraplist: limsEquipScraplistList) {
                    equipment.setEquipId(equipScraplist.getEquipId());                                //存入设备id
                    equipmentMapper.updateByPrimaryKeySelective(equipment);                         //修改设备数据
                }
                limsEquipScraplistMapper.updateByScraologId(limsEquipScraplist);                   //通过报废申请id修改报废设备信息
                count++;
            }
            if (count == idArray.length) {
                limsJson.setCode(0);
                limsJson.setMsg("审批成功！");
            } else {
                limsJson.setCode(1);
                limsJson.setMsg("审批失败！");
            }
        } else {
            limsJson.setCode(1);
            limsJson.setMsg("参数不正确！");
        }
        return limsJson;
    }


    /**
     * @作者: 祝依泽
     * @创建日期： 2019/10/14
     * @参数说明： String ids:报废申请id串
     * @方法介绍: 批量不批准与单个不批准共用方法
     */
    @Override
    public LimsJson nonPlacet(HttpServletRequest request, String ids) {
        LimsJson limsJson = new LimsJson(1,"err");                                      //封装返回数据工具
        if (ids != null && !(("").equals(ids))) {
            String[] idArray = ids.split(",");                                                //将ids用逗号分隔为id并存入数组
            LimsEquipScraplog limsEquipScraplog = new LimsEquipScraplog();
            LimsEquipScraplist limsEquipScraplist = new LimsEquipScraplist();
            Users user = UserUtil.getUser(request);                                                  //获取当前用户
            Date date = new Date();                                                                  //获取当前时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");       //转换时间格式方法
            int count = 0;                                                                           //用来记录删除数据的条数
            for (String idarr : idArray) {
                int scraplogId = Integer.valueOf(idarr);                                             //将String类型id转化为Integer类型id
                limsEquipScraplog.setScraplogId(scraplogId);                                         //主键id
                limsEquipScraplog.setApprover(user.getUserName());
                limsEquipScraplog.setApproverTime(format.format(date));
                limsEquipScraplog.setApprovalStatus("3");
                limsEquipScraplogMapper.updateByPrimaryKeySelective(limsEquipScraplog);
                limsEquipScraplist.setScraplogId(scraplogId);                                         //存入报废申请id，根据报废申请id修改数据
                limsEquipScraplist.setOpera(user.getUserName());                                      //存入审批人
                limsEquipScraplist.setOperaTime(new Date());                                          //存入审批时间
                limsEquipScraplistMapper.updateByScraologId(limsEquipScraplist);                   //通过报废申请id修改报废设备信息
                count++;
            }
            if (count == idArray.length) {
                limsJson.setCode(0);
                limsJson.setMsg("审批成功！");
            } else {
                limsJson.setCode(1);
                limsJson.setMsg("审批失败！");
            }
        } else {
            limsJson.setCode(1);
            limsJson.setMsg("参数不正确！");
        }
        return limsJson;
    }


    /**
     * @作者: 祝依泽
     * @创建日期： 2019/10/29
     * @参数说明：
     * String key: 字段名称（选择得查询字段）
     * String value: 查询条件（输入框输入得内容）
     * String status: （状态:0新建，1待批，2批准，3未批准）
     * @方法介绍: 通用得条件查询报废申请（搜索框功能）
     */
    @Override
    public LimsJson conditionQuery(HttpServletRequest request, String key, String value, String status) {
        LimsJson limsJson = new LimsJson(1,"err");                                      //封装返回数据工具
        if (key != null) {
            if (value != null && !(("").equals(value))){                                            //判断有误查询条件，有则按条件查询
                if (key.equals("deptId")) {                                                         //判断是否用deptId（部门）字段查询，通过部门名称模糊查询部门id，通过部门id查询数据
                    List array = new ArrayList<>();
                    List<Department> departmentList = departmentMapper.selectByDeptName(value);    //通过部门名称模糊查询部门信息
                    for (Department department : departmentList) {
                        List<LimsEquipScraplog> limsEquipScraplogList = limsEquipScraplogMapper.selectByDeptId(department.getDeptId()); //通过部门id查询申请数据
                        if (limsEquipScraplogList.size() > 0) array.add(limsEquipScraplogList);      //如果有数据，放入集合中
                    }
                    if (array.size() > 0) {
                        limsJson.setData(array);
                        limsJson.setCode(0);
                        limsJson.setMsg("查询成功！");
                    } else {
                        limsJson.setCode(0);
                        limsJson.setMsg("暂无数据！");
                    }
                } else {                                                                              //如果是其他字段查询，直接传入对应得字段和查询条件
                    Map<String, Object> map = new HashMap<>();                                        //存放查询条件
                    map.put(key, value);
                    map.put("status", status);
                    List<LimsEquipScraplog> limsEquipScraplogList = limsEquipScraplogMapper.selectByCondition(map); //查询数据
                    if (limsEquipScraplogList.size() > 0) {
                        limsJson.setData(limsEquipScraplogList);
                        limsJson.setCode(0);
                        limsJson.setMsg("查询成功！");
                    } else {
                        limsJson.setCode(0);
                        limsJson.setMsg("暂无数据！");
                    }
                }
            }
            if (value == null || (("").equals(value))){                                               //条件为空时，查询所有数据
                List<LimsEquipScraplog> limsEquipScraplogList = limsEquipScraplogMapper.selectByStatus(status); //查询数据
                if (limsEquipScraplogList.size() > 0) {
                    limsJson.setData(limsEquipScraplogList);
                    limsJson.setCode(0);
                    limsJson.setMsg("查询成功！");
                } else {
                    limsJson.setCode(0);
                    limsJson.setMsg("暂无数据！");
                }
            }
        }
        if (key == null) {
            limsJson.setCode(1);
            limsJson.setMsg("请选择查询条件！");
        }
        return limsJson;
    }
}
